local qiongbing = fk.CreateSkill {
  name = "lingling__qiongbing",
}

Fk:loadTranslationTable{
  ["lingling__qiongbing"] = "穷兵",
  [":lingling__qiongbing"] = "准备阶段，你可以令一名角色随机获得一张武器牌。",

  ["#lingling__qiongbing-choose"] = "穷兵：令一名角色随机获得一张武器牌",
}

qiongbing:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(qiongbing.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      min_num = 1,
      max_num = 1,
      prompt = "#lingling__qiongbing-choose",
      skill_name = qiongbing.name,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cards = table.filter(table.connect(room.draw_pile, room.discard_pile), function (id)
      return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
    end)
    for _, p in ipairs(room.alive_players) do
      for _, id in ipairs(p:getCardIds("he")) do
        if Fk:getCardById(id).sub_type == Card.SubtypeWeapon then
          table.insert(cards, id)
        end
      end
    end
    if #cards > 0 then
      room:moveCardTo(table.random(cards), Card.PlayerHand, to, fk.ReasonPrey, qiongbing.name, nil, true, to)
    end
  end,
})

return qiongbing
